package cn.com.fssg.platform.base.handler.impl.cl.tongji;

import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.biz.BizUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @className: Cldyq
 * @description: TODO 代用券
 * @author: xzk
 * @date: 2025/10/6
 **/
@Component
public class ClCostAllocationHandler implements InterfaceHandler {


//    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantMainJdbcTemplate();
    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {
        List<Map<String,Object>> list = new ArrayList<>();
        String type = jsonObject.getString("type");
        if(StringUtils.equals(type, "0")){
            StringBuffer sql = new StringBuffer();
            sql.append("SELECT * FROM cl_fee_report");
            Map<String,Object>  map = new HashMap<>();
            if(StringUtils.isNotBlank(jsonObject.getString("reportId"))){
                sql.append(" WHERE report_id =:report_id");
                map.put("report_id",jsonObject.getString("reportId"));
                list = jdbcTemplate.queryForList(sql.toString(),map);
                list.forEach(e->{
                    e.put("busTable",getBusTable(e));
                });
            }
        }else if(StringUtils.equals(type, "1")){
            StringBuffer sql = new StringBuffer();
            sql.append("SELECT * FROM cl_apply_history");
            String ft = jsonObject.getString("ft");
            Map<String,Object>  map = new HashMap<>();
            if(StringUtils.isNotBlank(jsonObject.getString("jiesuanMonth"))&&StringUtils.isNotBlank(jsonObject.getString("orgId"))){
                map.put("jiesuan_month",jsonObject.getString("jiesuanMonth"));
                map.put("org_id",jsonObject.getString("orgId"));
                sql.append(" WHERE jiesuan_month =:jiesuan_month  AND org_id=:org_id");
                if(StringUtils.equals(ft, "1")){
                    sql.append(" AND report_id =:report_id");
                    map.put("report_id","0");
                }
                list = jdbcTemplate.queryForList(sql.toString(),map);
                list.forEach(e->{
                    e.put("busTable",getBusTable(e));
                });
            }
        }else if(StringUtils.equals(type, "2")){
            StringBuffer sql = new StringBuffer();
            sql.append("SELECT * FROM cl_apply_history");
            String ft = jsonObject.getString("ft");
            Map<String,Object>  map = new HashMap<>();
            if(StringUtils.isNotBlank(jsonObject.getString("jiesuanMonthRange"))&&StringUtils.isNotBlank(jsonObject.getString("orgId"))){
                String jiesuanMonthRange  = jsonObject.getString("jiesuanMonthRange");
                String start []= jiesuanMonthRange.split(",");
                map.put("org_id",jsonObject.getString("orgId"));
                String querySql=" WHERE org_id =:org_id AND jiesuan_month >= '%s' AND jiesuan_month <= '%s' ";
                querySql = String.format(querySql, start[0], start[1]);
                sql.append(querySql);
                if(StringUtils.equals(ft, "1")){
                    sql.append(" AND report_id =:report_id");
                    map.put("report_id","0");
                }
                if(StringUtils.isNotBlank(jsonObject.getString("cjrHukouLike"))){
                    sql.append(" AND cjr_hukou like '%");
                    sql.append(jsonObject.getString("cjrHukouLike"));
                    sql.append("%'");
                }

                if(StringUtils.isNotBlank(jsonObject.getString("cjrName"))){
                    sql.append(" AND cjr_name=:cjr_name");
                    map.put("cjr_name",jsonObject.getString("cjrName"));
                }
                if(StringUtils.isNotBlank(jsonObject.getString("shenpiNo"))){
                    sql.append(" AND shenpi_no=:shenpi_no");
                    map.put("shenpi_no",jsonObject.getString("shenpiNo"));
                }
                if(StringUtils.isNotBlank(jsonObject.getString("applyType"))){
                    sql.append(" AND apply_type=:apply_type");
                    map.put("apply_type",jsonObject.getString("applyType"));
                }
                if(StringUtils.isNotBlank(jsonObject.getString("cjrServiceCat"))) {
                    sql.append(" AND cjr_service_cat=:cjr_service_cat");
                    map.put("cjr_service_cat",jsonObject.getString("cjrServiceCat"));
                }
                list = jdbcTemplate.queryForList(sql.toString(),map);
                list.forEach(e->{
                    e.put("busTable",getBusTable(e));
                });
            }
        }
        return list;
    }

    public Object getBusTable(Map<String,Object> i) {
        if (i.get("apply_type").toString().equalsIgnoreCase("APPLY_XUNLIAN")) {
            return getApplyId(i.get("apply_id").toString(),"cl_apply_xunlian");
        } else if (i.get("apply_type").toString().equalsIgnoreCase("APPLY_BIANGEN")) {
            return getApplyId(i.get("apply_id").toString(),"cl_apply_change");
        } else if (i.get("apply_type").toString().equalsIgnoreCase("APPLY_FUJU")) {
            return getApplyId(i.get("apply_id").toString(),"cl_apply_fuju");
        } else if (i.get("apply_type").toString().equalsIgnoreCase("APPLY_FUJU_JIESUAN")) {
            return getApplyId(i.get("apply_id").toString(),"cl_apply_fuju_org_jiesuan");
        } else if (i.get("apply_type").toString().equalsIgnoreCase("APPLY_XUNLIAN_JIESUAN")) {
            return getApplyId(i.get("apply_id").toString(),"cl_apply_xunlian_org_jiesuan");
        } else {
            return i.get("apply_type").toString().equalsIgnoreCase("APPLY_XUNLIAN_CHANGE_JIESUAN") ? getApplyId(i.get("apply_id").toString(),"cl_apply_change_org_jiesuan") : null;
        }
    }

    public Object getApplyId(String id,String table) {
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("id",id);
        String sql = "SELECT * FROM ";
        sql +=  table+ " WEHRE id = :id";
        Map<String,Object> map = jdbcTemplate.queryForMap(sql,queryMap);
        return map;
    }
}